app.controller("z-special-c", [ '$scope','$rootScope','$z-http','$sce','z-special-s','z-dictionary-s','z-corporation-s','z-department-s','z-punchTime-s','z-attendance-s','z-employee-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,DepartmentService,PunchTimeService,AttendanceService,EmployeeService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.specialDateOpened= false;
		
		//编辑使用
		$scope.editModel = {};
		$scope.editModel.special={};
		$scope.editModel.special.specialId=null;
		$scope.editModel.special.employeeIdList=[];
		$scope.editModel.title="";
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'special'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//起止日期选择
		$scope.viewModel.specialDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			 $scope.viewModel.specialDateOpened = !$scope.viewModel.specialDateOpened;
		};
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getSpecialList({
				pageCount : $scope.viewModel.perPage,
				pageNo : $scope.viewModel.rowCount,
				specialName:$scope.viewModel.specialName,
			},
			function(data){
				$scope.viewModel.dataList = data.specialBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		 //格式化验证
        $scope.viewModel.escapeRegExp=function(string){
	        var pattern=/^([0-9]|[0-1][0-9]|2[0-3]):(2[0-9]|[0-5][0-9])$/;
	        var flag = pattern.test(string);
	        return flag;
        };
        
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
			$('#spStartTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					$scope.editModel.special.startTime=$('#spStartTime').val();
					$scope.editForm.startTime.$valid=true;
					$scope.editForm.startTime.$dirty=true;
					
				}	
			});
			$('#spEndTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					$scope.editModel.special.endTime=$('#spEndTime').val();
					$scope.editForm.endTime.$valid=true;
					$scope.editForm.endTime.$dirty=true;
				}	
			});
		};
		
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.editModel.title="新增特殊情况";
			$scope.editModel.type = true;
			$scope.editModel.special={};
			$scope.editModel.special.specialId=null;
			$scope.editModel.special.employeeIdList=[];
			$scope.editModel.special.startTime=null;
			$scope.editModel.special.endTime=null;
			
			//默认员工集合
			$scope.viewEmployeeModel.checkboxDataList=[];
			$scope.viewEmployeeModel.isDisabledData=[];
			//验证不能为空
			$scope.editForm.specialDate.$dirty=false;
			$scope.editForm.specialName.$dirty=false;
			$scope.editForm.startTime.$dirty=false;
			$scope.editForm.endTime.$dirty=false;
			$scope.viewModel.load();
		};
		
		//修正
		$scope.viewModel.update=function(special){
			$scope.editModel.title="修改特殊情况";
			$scope.editModel.type = false;
			$scope.editModel.special=angular.copy(special);
			//获取当前日期
	        var specialDate= $filter("date")($scope.editModel.special.specialDate, "yyyy/MM/dd");
	        $scope.editModel.special.specialDate=new Date(specialDate);
	        $scope.viewModel.isLoading = true;
			Service.getEmployeeIdListBySpecialId({
				specialId:special.specialId
			},
			function(data){
				$scope.viewEmployeeModel.specialDateType=data.specialDateType;
				$scope.editModel.special.employeeIdList=data.employeeIdList;
				//默认员工集合
				$scope.viewEmployeeModel.checkboxDataList=[];
				$scope.viewEmployeeModel.isDisabledData=[];
				for(var i=0;i<$scope.editModel.special.employeeIdList.length;i++){
					var cheDate={};
					cheDate.employeeId=$scope.editModel.special.employeeIdList[i];
					cheDate.checkbox=true;
					cheDate.specialDateType=$scope.viewEmployeeModel.specialDateType;
					$scope.viewEmployeeModel.checkboxDataList.push(cheDate);
					$scope.viewEmployeeModel.isDisabledData.push($scope.editModel.special.employeeIdList[i]);
				}
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading = false;
			});
			$scope.viewModel.load();
		};
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.specialDate.$dirty){
            		$scope.editForm.specialDate.$dirty=true;
            	}
            	if(!$scope.editForm.specialName.$dirty){
            		$scope.editForm.specialName.$dirty=true;
            	}
            	if(!$scope.editForm.startTime.$dirty){
            		$scope.editForm.startTime.$dirty=true;
            	}
            	if(!$scope.editForm.endTime.$dirty){
            		$scope.editForm.endTime.$dirty=true;
            	}
                return true;
            }else{
            	if($scope.editModel.special.employeeIdList.length<=0){
            		$rootScope.alertWarning("未选择人员！");
    				return true;
            	}
            	
            	//开始时间和结束时间必须要有一个是不为空的，但两者都空不可
            	if(($scope.editModel.special.startTime==null || $scope.editModel.special.startTime=="" || $.isEmptyObject($scope.editModel.special.startTime))
            			&& ($scope.editModel.special.endTime==null || $scope.editModel.special.endTime=="" || $.isEmptyObject($scope.editModel.special.endTime))){
            		$rootScope.alertWarning("开始时间和结束时间必须有一个必填！");
    				return true;
            	}
            	//如果开始时间不为空，那么接下来就判断是否符合时间格式
            	if($scope.editModel.special.startTime!=null && $scope.editModel.special.startTime!=""
            		&& !$.isEmptyObject($scope.editModel.special.startTime)){
            		if(!$scope.viewModel.escapeRegExp($scope.editModel.special.startTime)){
            			$scope.editForm.startTime.$dirty=true;
            			$rootScope.alertWarning("开始时间格式不正确。");
            			return true;
            		}
            	}
            	//如果结束时间不为空，那么接下来就判断是否符合时间格式
            	if($scope.editModel.special.endTime!=null && $scope.editModel.special.endTime!=""
            		 && !$.isEmptyObject($scope.editModel.special.endTime)){
            		if(!$scope.viewModel.escapeRegExp($scope.editModel.special.endTime)){
            			$scope.editForm.endTime.$dirty=true;
            			$rootScope.alertWarning("结束时间格式不正确。");
            			return true;
            		}
            	}
            }
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			
			//删除重复人员ID
			var employeeIdList=[];
			for (var i = 0; i < $scope.editModel.special.employeeIdList.length; i++) {
			     if(employeeIdList.indexOf($scope.editModel.special.employeeIdList[i])==-1){
			    	 employeeIdList.push($scope.editModel.special.employeeIdList[i]);
			     }
		    }
			
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveSpecial({
					employeeIdList:employeeIdList,
					startTime:$scope.editModel.special.startTime,
					endTime:$scope.editModel.special.endTime,
					specialDate:$scope.editModel.special.specialDate,
					specialName:$scope.editModel.special.specialName,
					description:$scope.editModel.special.description,
				},
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}else{
				Service.updateSpecial({
					specialId:$scope.editModel.special.specialId,
					employeeIdList:employeeIdList,
					startTime:$scope.editModel.special.startTime,
					endTime:$scope.editModel.special.endTime,
					specialDate:$scope.editModel.special.specialDate,
					specialName:$scope.editModel.special.specialName,
					description:$scope.editModel.special.description,
					updateTime:$scope.editModel.special.updateTime,
				},
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}
		};
		
		/************人员*************/
		//选择员工
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.employeeNo="";
	    $scope.viewEmployeeModel.departmentName="";
	    $scope.viewEmployeeModel.corporationName="";
	    $scope.viewEmployeeModel.roleName="";
		$scope.viewEmployeeModel.perPage="1";
		$scope.viewEmployeeModel.rowCount="10";
		$scope.viewEmployeeModel.title="选择人员";
		$scope.viewEmployeeModel.specialDateType=false;
		
		// 定义当前时间
		$scope.viewEmployeeModel.effectiveDate = new Date();
		$scope.viewEmployeeModel.employeeCount=0;
		//默认员工集合
		$scope.viewEmployeeModel.checkboxDataList=[];
		//不可编辑使用
		$scope.viewEmployeeModel.isDisabledData=[];
		//加载
		$scope.viewEmployeeModel.load=function(){
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			$scope.viewEmployeeModel.level=null;
			$(".multiselect-selected-text").text("无");
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			//加载信息
			$scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
				//查询公司
				CorporationService.getCorporationList({
					fromDate:$scope.viewEmployeeModel.effectiveDate,
				},
				function(data){
					var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.corporationCode+")"+values.corporationName,  
			                value : values.corporationCode  
			            });  
			        });  
			        $('#speCorporation').multiselect('dataprovider',site);
				},function(error) {
					
				});
			};
			$scope.viewEmployeeModel.bindSelectRegionTypeData();
			
		
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#spelevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#speCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#speCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewEmployeeModel.effectiveDate;
			    if(value==null){
			    	var site = [];  
			    	$('#speDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#speDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#speDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			$scope.viewEmployeeModel.bindData();
		};
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
			$scope.viewEmployeeModel.employeeNo="";
		    $scope.viewEmployeeModel.departmentName="";
		    $scope.viewEmployeeModel.corporationName="";
		    $scope.viewEmployeeModel.roleName="";
		};
		
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			
			//获取特殊情况ID
			var specialId=$scope.editModel.special.specialId;
			var specialSortType=null;
			if(specialId!=null){
				specialSortType=1;
			}
			
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				roleName:$scope.viewEmployeeModel.roleName,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				specialSortType:specialSortType,
				specialId:specialId,
				/*employeeIsSeparated:"0",*/
				perPage:$scope.viewEmployeeModel.perPage,
				rowCount:$scope.viewEmployeeModel.rowCount
			},
			function(data){
				$scope.viewEmployeeModel.dataList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.addCheckbox();
				$scope.viewEmployeeModel.loadDefaultcheckbox();
			    $scope.viewEmployeeModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//选择人员保存
		$scope.viewEmployeeModel.saveEmployee=function(){
			$scope.editModel.special.employeeIdList=[];
			for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
					 $scope.editModel.special.employeeIdList.push($scope.viewEmployeeModel.checkboxDataList[i].employeeId);
				 }
			}
			$scope.viewEmployeeModel.unLoad();
		};
		
		//默认全员分配
		$scope.viewEmployeeModel.allEmployee=function(){
			//获取基本信息
			EmployeeService.getEmployeeIdList({
				employeeNo:$scope.viewEmployeeModel.employeeNo,
				departmentName:$scope.viewEmployeeModel.departmentName,
				corporationName:$scope.viewEmployeeModel.corporationName,
				employeeIsSeparated:"0"
			},
			function(data){
				$scope.editModel.special.employeeIdList=data;
				$scope.viewEmployeeModel.checkboxDataList=[];
				for(var i=0;i<$scope.editModel.special.employeeIdList.length;i++){
					var cheDate={};
					cheDate.employeeId=$scope.editModel.special.employeeIdList[i];
					cheDate.checkbox=true;
					$scope.viewEmployeeModel.checkboxDataList.push(cheDate);
				}
				$scope.viewEmployeeModel.unLoad();
			},function(error) {
				$rootScope.alertError(error.fault.message);
			});
		};
		
		
		
		//设置默认复选框值
		$scope.viewEmployeeModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewEmployeeModel.dataList.length;i++){
				 $scope.viewEmployeeModel.dataList[i].checkbox=false;
			 }
		};
		
		//处理加载复选框选择状态
		$scope.viewEmployeeModel.loadDefaultcheckbox=function(){
			 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				 for(var j=0;j<$scope.viewEmployeeModel.dataList.length;j++){
					 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==$scope.viewEmployeeModel.dataList[j].employeeId){
						 if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
							 $scope.viewEmployeeModel.dataList[j].checkbox=true;
						 }else{
							 $scope.viewEmployeeModel.dataList[j].checkbox=false;
						 }
					 }
				 }
			 }
		};
		
		//判断是否为全选
		$scope.viewEmployeeModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewEmployeeModel.dataList.length;i++){
				 if($scope.viewEmployeeModel.dataList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.viewEmployeeModel.checkAll=true;
			 }else{
				 $scope.viewEmployeeModel.checkAll=false;
			 }
		};
		
		//全选事件
		$scope.viewEmployeeModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewEmployeeModel.checkAll==false){
				 angular.forEach($scope.viewEmployeeModel.dataList, function(value, key) {
					 if(!$scope.viewEmployeeModel.isDisabled(value)){
						 value.checkbox=true;
						 var type=true;
						 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
							 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==value.employeeId
									 && $scope.viewEmployeeModel.checkboxDataList[i].specialDateType==false){
								 type=false;
								 break;
							 }else{
								 type=true;
							 }
						 }
						 if(type){
							 $scope.viewEmployeeModel.checkboxDataList.push(value);
						 }
					 }
				 });
			 }else{
				 angular.forEach($scope.viewEmployeeModel.dataList, function(value, key) {
					 if(!$scope.viewEmployeeModel.isDisabled(value)){
						 value.checkbox=false;
						 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
							 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==value.employeeId){
								 $scope.viewEmployeeModel.checkboxDataList.splice(i,1);
							 }
						 }
					 }
				 });
			 }
		};
		
		//单选事件
		$scope.viewEmployeeModel.checkbox=function(employee){
			 if(employee.checkbox){
				 employee.checkbox=false;
				 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
					 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==employee.employeeId){
						 $scope.viewEmployeeModel.checkboxDataList.splice(i,1);
					 }
				 }
			 }else{
				 employee.checkbox=true;
				 $scope.viewEmployeeModel.checkboxDataList.push(employee);
			 }
			 $scope.viewEmployeeModel.JudgeCheckboxAll();
		};
		
		//判断是否可用
		$scope.viewEmployeeModel.isDisabled=function(employee){
			for(var i=0;i<$scope.viewEmployeeModel.isDisabledData.length;i++){
				if($scope.viewEmployeeModel.isDisabledData[i]==employee.employeeId && $scope.viewEmployeeModel.specialDateType==true){
					return true;
				}
			}
			return false;
		};
		
		/**************选择考勤打卡时间**************/
		$scope.viewPunchTimeModel = {};
		$scope.viewPunchTimeModel.title="选择考勤时间";
		
		//默认员工集合
		$scope.viewPunchTimeModel.radioData=null;
		
		//选中
		$scope.viewPunchTimeModel.radio=function(punchTime){
			$scope.viewPunchTimeModel.radioData=angular.copy(punchTime);
		};
		
		//加载
		$scope.viewPunchTimeModel.load=function(){
			$scope.viewPunchTimeModel.bindData();
			$scope.viewPunchTimeModel.show=true;
			//默认员工集合
			$scope.viewPunchTimeModel.radioData=null;
		};
		
		//关闭加载
		$scope.viewPunchTimeModel.unLoad=function(){
			$scope.viewPunchTimeModel.show=false;
		};
		
		//加载数据
		$scope.viewPunchTimeModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			PunchTimeService.getPunchTimeList({},
			function(data){
				$scope.viewPunchTimeModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//单选框是否选中
		$scope.viewPunchTimeModel.rdoChecked=function(punchTime){
			if($scope.editModel.special.punchTimeId!=null){
				if(punchTime.punchTimeId==$scope.editModel.special.punchTimeId){
					return true;
				}
			}
			return false;
		};
		
		//选择人员保存
		$scope.viewPunchTimeModel.save=function(){
			if($scope.viewPunchTimeModel.radioData!=null){
				$scope.editModel.special.punchTimeId=$scope.viewPunchTimeModel.radioData.punchTimeId;
				$scope.editModel.special.startFromTime=$scope.viewPunchTimeModel.radioData.startFromTime;
				$scope.editModel.special.startToTime=$scope.viewPunchTimeModel.radioData.startToTime;
				$scope.editModel.special.startInterval=$scope.viewPunchTimeModel.radioData.startInterval;
				$scope.editModel.special.endInterval=$scope.viewPunchTimeModel.radioData.endInterval;
				$scope.editModel.special.endFromTime=$scope.viewPunchTimeModel.radioData.endFromTime;
				$scope.editModel.special.endToTime=$scope.viewPunchTimeModel.radioData.endToTime;
			}
			$scope.viewPunchTimeModel.unLoad();
		};
		
} ]);